package com.voice.call.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.voice.call.common.Result;
import com.voice.call.entity.VideoLibrary;
import com.voice.call.mapper.VideoLibraryMapper;
import com.voice.call.service.VideoLibraryService;

@Service
public class VideoLibraryServiceImpl implements VideoLibraryService{

	@Resource
	private VideoLibraryMapper videoLibraryMapper;
	
	@Override
	public Result getVideoList(Integer curpage,Integer pagesize){
		
		int total = 0;
		int maxpage = 0;
		int pageId = 0;
		Map<String, Object> totalMap = this.getTotalMap();
		if (totalMap!=null && !totalMap.isEmpty()) {
			total = Integer.valueOf(totalMap.get("total").toString()).intValue();
			pageId = Integer.valueOf(totalMap.get("pageId").toString()).intValue();
		}else {
			total = videoLibraryMapper.count();
		}
		 maxpage = total%pagesize == 0?total/pagesize : (total/pagesize)+1;
		
		 
		
		Map<String,Object> newsMap = new HashMap<String, Object>();
		newsMap.put("page", curpage);
		newsMap.put("pagesize", pagesize);
		newsMap.put("maxpage", maxpage);
		newsMap.put("total", total);
		
		if (curpage>maxpage) {
			newsMap.put("list", new ArrayList<Map<String,Object>>());
			return new Result(newsMap);
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("curpage", (curpage-1)*pagesize);
		params.put("pagesize", pagesize);
		params.put("id", pageId);
		List<VideoLibrary> list = videoLibraryMapper.list(params);
		List<Map<String, Object>> rsList = new ArrayList<Map<String,Object>>();
		if (list!=null && !list.isEmpty()) {
			for (VideoLibrary item : list) {
				Map<String, Object> itemMap = new HashMap<String, Object>();
				itemMap.put("id", item.getId());
				itemMap.put("url", item.getUrl());
				itemMap.put("image", item.getPhoto());
				rsList.add(itemMap);
			}
		}
		
		
		newsMap.put("list", rsList);
		
		return new Result(newsMap);
	}
	
	private Map<String, Object> getTotalMap() {
		 Calendar cal = Calendar.getInstance();
	     int day=cal.get(Calendar.DATE);//获取日
	     
	     Map<String, Object> params = new HashMap<String, Object>();
	     params.put("day",day);
	     return videoLibraryMapper.getVideoCountMap(params);
	    
	}
	
	
}
